package com.mall.goods.service.impl;

import com.mall.common.exception.ServiceException;
import com.mall.common.utils.DateUtils;
import com.mall.common.utils.SecurityUtils;
import com.mall.goods.mapper.GoodsMapper;
import com.mall.goods.mapper.GoodsSpecsMapper;
import com.mall.goods.model.dto.GoodsDto;
import com.mall.goods.model.dto.GoodsSpecsDto;
import com.mall.goods.model.vo.GoodsSpecsVo;
import com.mall.goods.model.vo.GoodsVo;
import com.mall.goods.service.IGoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class GoodsServiceImpl implements IGoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Resource
    private GoodsSpecsMapper goodsSpecsMapper;

    /**
     * 查询商品
     */
    @Override
    public GoodsVo selectGoodsById(Long id)
    {
        GoodsVo goodsVo = goodsMapper.selectGoodsById(id);
        if (null != goodsVo) {
            GoodsSpecsDto goodsSpecsDto = new GoodsSpecsDto();
            goodsSpecsDto.setGoodsId(goodsVo.getId());
            List<GoodsSpecsVo> goodsSpecsVos = goodsSpecsMapper.selectGoodsSpecsList(goodsSpecsDto);
            if (null != goodsSpecsVos && goodsSpecsVos.size() > 0) {
                goodsVo.setSellingPrice(goodsSpecsVos.get(0).getSpecsPrice());
                goodsVo.setOriginalPrice(goodsSpecsVos.get(0).getSpecsOriginPrice());
                goodsVo.setStockNum(goodsSpecsVos.get(0).getSpecsStockNum());
                goodsVo.setSpecsList(goodsSpecsVos);

                goodsVo.setSpecsId(goodsSpecsVos.get(0).getId());
                goodsVo.setSpecsName(goodsSpecsVos.get(0).getSpecsName());
            }
        }
        return goodsVo;
    }

    /**
     * 查询商品列表
     */
    @Override
    public List<GoodsVo> selectGoodsList(GoodsDto goodsDto)
    {
        List<GoodsVo> goodsVos = goodsMapper.selectAppGoodsList(goodsDto);
        handleGoodsList(goodsVos);
        return goodsVos;
    }

    /**
     * 新增商品
     */
    @Transactional
    @Override
    public int insertGoods(GoodsDto goodsDto)
    {
        if ("1".equals(goodsDto.getSpecsFlag())) {
            if (null == goodsDto.getSpecsList() || goodsDto.getSpecsList().size() == 0) {
                throw new ServiceException("商品多规格不能为空");
            }
        }

        goodsDto.setCreateTime(DateUtils.getNowDate());
        goodsDto.setCreateBy(SecurityUtils.getNickName());
        goodsDto.setCreateId(SecurityUtils.getUserId());
        int row = goodsMapper.insertGoods(goodsDto);
        if ("1".equals(goodsDto.getSpecsFlag())) {
            goodsDto.getSpecsList().forEach(goodsSpecsDto -> {
                goodsSpecsDto.setGoodsId(goodsDto.getId());
            });
            goodsSpecsMapper.batchInsertGoodsSpecs(goodsDto.getSpecsList());
        }
        return row;
    }

    /**
     * 修改商品
     */
    @Override
    public int updateGoods(GoodsDto goodsDto)
    {
        if ("1".equals(goodsDto.getSpecsFlag())) {
            if (null == goodsDto.getSpecsList() || goodsDto.getSpecsList().size() == 0) {
                throw new ServiceException("商品多规格不能为空");
            }
        }

        goodsDto.setUpdateTime(DateUtils.getNowDate());
        goodsDto.setUpdateBy(SecurityUtils.getNickName());
        int row = goodsMapper.updateGoods(goodsDto);

        goodsSpecsMapper.deleteGoodsSpecsByGoodsId(goodsDto.getId());

        if ("1".equals(goodsDto.getSpecsFlag())) {
            goodsDto.getSpecsList().forEach(goodsSpecsDto -> {
                goodsSpecsDto.setGoodsId(goodsDto.getId());
            });
            goodsSpecsMapper.batchInsertGoodsSpecs(goodsDto.getSpecsList());
        }
        return row;
    }

    /**
     * 批量删除商品
     */
    @Override
    public int deleteGoodsByIds(Long[] ids)
    {
        return goodsMapper.deleteGoodsByIds(ids);
    }

    /**
     * 删除商品信息
     */
    @Override
    public int deleteGoodsById(Long id)
    {
        return goodsMapper.deleteGoodsById(id);
    }


    @Override
    public List<GoodsVo> selectAppGoodsList(GoodsDto goodsDto) {
        List<GoodsVo> goodsVos = goodsMapper.selectAppGoodsList(goodsDto);
        handleGoodsList(goodsVos);
        return goodsVos;
    }

    private void handleGoodsList(List<GoodsVo> goodsVos) {
        if (null != goodsVos && goodsVos.size() > 0) {
            List<Long> goodsIds = new ArrayList<>();
            goodsVos.forEach(goodsVo -> {
                goodsIds.add(goodsVo.getId());
            });
            List<GoodsSpecsVo> goodsSpecsVos = goodsSpecsMapper.selectGoodsSpecsListByGoodsIds(goodsIds);
            if (null != goodsSpecsVos && goodsSpecsVos.size() > 0) {
                goodsVos.forEach(goodsVo -> {
                    if ("1".equals(goodsVo.getSpecsFlag())) {
                        goodsSpecsVos.forEach(goodsSpecsVo -> {
                            if (goodsSpecsVo.getGoodsId().longValue() == goodsVo.getId().longValue()) {
                                if (null == goodsVo.getSpecsList()) {
                                    goodsVo.setSpecsList(new ArrayList<>());
                                }
                                goodsVo.getSpecsList().add(goodsSpecsVo);
                            }
                        });
                        if (goodsVo.getSpecsList().size() > 0) {
                            goodsVo.setSellingPrice(goodsVo.getSpecsList().get(0).getSpecsPrice());
                            goodsVo.setOriginalPrice(goodsVo.getSpecsList().get(0).getSpecsOriginPrice());
                            goodsVo.setStockNum(goodsVo.getSpecsList().get(0).getSpecsStockNum());
                        }
                    }
                });
            }
        }
    }
}
